/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.managedbeans;

import com.bionic.ce.entities.Bankacc;
import com.bionic.ce.entitybeans.bankacc.BankAccFacadeLocal;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "bankAccManagedBean")
@SessionScoped
public class BankAccManagedBean implements Serializable {

    public BankAccManagedBean() {
        bankAcc = new Bankacc();
    }
    Bankacc bankAcc = new Bankacc();
    @EJB
    private BankAccFacadeLocal bankAccEJB;
    List<Bankacc> bankAccList = new ArrayList<Bankacc>();
    @ManagedProperty("#{userManagedBean}")
    private UserManagedBean userMB;
    @ManagedProperty("#{currencyManagedBean}")
    private CurrencyManagedBean currencyMB;

    public BankAccFacadeLocal getBankAccEJB() {
        return bankAccEJB;
    }

    public void setBankAccEJB(BankAccFacadeLocal bankAccEJB) {
        this.bankAccEJB = bankAccEJB;
    }

    public Bankacc getBankAcc() {
        return bankAcc;
    }

    public void setBankAcc(Bankacc bankAcc) {
        this.bankAcc = bankAcc;
    }

    public UserManagedBean getUserMB() {
        return userMB;
    }

    public void setUserMB(UserManagedBean userMB) {
        this.userMB = userMB;
    }

    public CurrencyManagedBean getCurrencyMB() {
        return currencyMB;
    }

    public void setCurrencyMB(CurrencyManagedBean currencyMB) {
        this.currencyMB = currencyMB;
    }

    public List<Bankacc> getBankAccList() {
        return bankAccList;
    }

    public void setBankAccList(List<Bankacc> bankAccList) {
        this.bankAccList = bankAccList;
    }

    //создание банковского счёта
    public String doCreateBankAcc() {
        bankAcc.setUserid(userMB.user);
        bankAccEJB.create(bankAcc);
        bankAcc = new Bankacc();
        return doShowList();
    }
    //обновление данных банковского счёта

    public String doUpdateBankAcc() {
        bankAccEJB.edit(bankAcc);
        bankAcc = new Bankacc();
        return doShowList();
    }
    
    //удаление данных банковского счёта

    public String doDeleteBankAcc() {
        bankAccEJB.remove(bankAcc);
        bankAcc = new Bankacc();
        return doShowList();
    }
    // список зарегистрированных банковских счётов для данного пользователя

    public String doShowList() {
        bankAccList = bankAccEJB.findByUserIdAndCurrency(userMB.getUser().getId(), null);
        return "/pages/bankacc/bankaccountsinfo.xhtml?faces-redirect=true";
    }

    public String goToAddBankAcc() {
        bankAcc = new Bankacc();
        return "/pages/bankacc/registerbankacc.xhtml?faces-redirect=true";
    }

    public String goToSelectedBankAccInfo() {
        if (bankAcc.getAccNum() != null) {
            return "/pages/bankacc/bankaccinfo.xhtml?faces-redirect=true";
        } else {
            return null;
        }
    }
}
